//
//  XMHomePageEditViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/13.
//

#import "XMHomePageEditViewController.h"
#import "XMHomePageEditView.h"
#import "XMNetwork+mine.h"
#import "XMInfoEditViewController.h"
#import "XMUserInfoModel.h"
#import "XMHAreaModel.h"
#import "XMAreaSelectViewController.h"
#import "XMJobSelectViewController.h"
#import <Photos/Photos.h>

@interface XMHomePageEditViewController ()<XMHomePageEditViewDelegate>

@property (nonatomic, strong) XMHomePageEditView *homePageEditView;

@property (nonatomic, strong) XMUserInfoModel *userInfoModel;

@property (nonatomic, copy) NSString *avatarStr;

@end

@implementation XMHomePageEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        
    }];
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.homePageEditView];
    [self getHomePageInfo];
}

//获取基本信息
- (void)getHomePageInfo {
    XMWeakSelf;
    [_Network getUserInfoSuccess:^(id  _Nonnull responseObject) {
        weakSelf.userInfoModel = [XMUserInfoModel yy_modelWithJSON:responseObject[@"data"]];
        [weakSelf.homePageEditView setContentWithUserInfo:weakSelf.userInfoModel];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//修改基本信息
- (void)changeBasicInfo {
    XMWeakSelf;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters safe_emptyObject:_userInfoModel.nickname forKey:@"nickname"];
    [parameters safe_emptyObject:_userInfoModel.dsc forKey:@"dsc"];
    [parameters safe_emptyObject:_userInfoModel.city forKey:@"city"];
    [parameters safe_emptyObject:_userInfoModel.province forKey:@"province"];
//    [parameters safe_emptyObject:@"1338990988" forKey:@"idNo"];
    [parameters safe_emptyObject:@"1993-05-15" forKey:@"birthday"];
    [parameters safe_emptyObject:_userInfoModel.school forKey:@"school"];
    [parameters safe_emptyObject:_userInfoModel.xueyuan forKey:@"xueyuan"];
    [parameters safe_emptyObject:@"2025-05-15" forKey:@"startSchoolTime"];
//    [parameters safe_emptyObject:@"本科" forKey:@"xueli"];
    [parameters safe_emptyObject:_userInfoModel.sex forKey:@"sex"];
    [parameters safe_emptyObject:_userInfoModel.avatar forKey:@"avatar"];
    [_Network changeBasicInfoWithBasicInfo:parameters
                                   success:^(id  _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"修改成功"];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//提交头像
- (void)uploadAvatar:(UIImage *)avatarImg {
    XMWeakSelf;
    [_Network updateAvatarWithImg:avatarImg
                          success:^(id  _Nonnull repsonseObject) {
        weakSelf.userInfoModel.avatar = [repsonseObject safeStringForKey:@"data"];
        [weakSelf.homePageEditView setSelectImg:weakSelf.userInfoModel.avatar];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//获取区域信息
- (void)getAreaList {
    XMWeakSelf;
    [_Network getAreaListSuccess:^(id  _Nonnull responseObject) {
        NSArray *areaModelArr = [NSArray yy_modelArrayWithClass:[XMHAreaModel class] json:responseObject[@"data"]];
        XMAreaSelectViewController *areaSelectVC = [[XMAreaSelectViewController alloc] init];
        areaSelectVC.areaModelArr = areaModelArr;
        areaSelectVC.selectBlock = ^(NSString * _Nonnull areaStr, NSString * _Nonnull areaId) {
            if (areaStr.length > 0) {
                weakSelf.userInfoModel.province = [areaStr componentsSeparatedByString:@"-"][0];
                weakSelf.userInfoModel.city = [areaStr componentsSeparatedByString:@"-"][1];
            }
            [weakSelf.homePageEditView setContentWithUserInfo:weakSelf.userInfoModel];
        };
        XMBaseNavigationController *areaNVC = [[XMBaseNavigationController alloc] initWithRootViewController:areaSelectVC];
        areaNVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [weakSelf presentViewController:areaNVC animated:YES completion:nil];
//        [weakSelf.navigationController pushViewController:areaSelectVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
//选择修改项
- (void)itemSelect:(NSInteger)itemIndex {
    if (itemIndex == 5) {
        [self getAreaList];
    }
    else if (itemIndex == 7) {
        XMJobSelectViewController *jobSelectVC = [[XMJobSelectViewController alloc] init];
        jobSelectVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:jobSelectVC animated:YES];
    }
    else {
        XMInfoEditViewController *infoEditVC = [[XMInfoEditViewController alloc] init];
        infoEditVC.userInfoModel = _userInfoModel;
        infoEditVC.editIndex = itemIndex;
        [self.navigationController pushViewController:infoEditVC animated:YES];
        
        XMWeakSelf;
        infoEditVC.infoSaveBlock = ^{
            [weakSelf.homePageEditView setContentWithUserInfo:weakSelf.userInfoModel];
        };
    }
}

//提交修改
- (void)confirmChange {
    [self changeBasicInfo];
}

- (void)updateAvatarWithImg:(UIImage *)avatarImg {
    [self uploadAvatar:avatarImg];
}

#pragma mark - Init
- (XMHomePageEditView *)homePageEditView {
    if (!_homePageEditView) {
        _homePageEditView = [[XMHomePageEditView alloc] initWithFrame:self.view.bounds];
        _homePageEditView.delegate = self;
    }
    return _homePageEditView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
